package com.shanyutiwen.springbootblogtest.service;

import com.shanyutiwen.springbootblogtest.dao.Order;
import com.shanyutiwen.springbootblogtest.dao.UserMapper;
import com.shanyutiwen.springbootblogtest.model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class UserService {

//    public static void main(String[] args) throws IOException {
//        final InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
//        final SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//        final SqlSession sqlSession = sqlSessionFactory.openSession(true);
//        final UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//        final List<User> user = userMapper.findByIds(ids);
//        sqlSession.close();
//        return user;

//        final InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
//        final SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//        final SqlSession sqlSession = sqlSessionFactory.openSession(true);
//        final UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//        final List<User> all = userMapper.findAll();
//        for(User user : all){
//            System.out.println(user.getUsername());
//            List<Order> orderList = user.getOrderList();
//            for(Order order : orderList){
//                System.out.println(order);
//            }
//            System.out.println("----------------------------------");
//        }
//
//        sqlSession.close();
//    }

//    public User findById(Long id) throws IOException {
////        final InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
////        final SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
////        final SqlSession sqlSession = sqlSessionFactory.openSession(true);
////
////        final UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
////        final User user = userMapper.findById(id);
////        sqlSession.close();
////        return user;
//    }
//
//    public List<User> getAll() throws IOException {
////        final InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
////        final SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
////        final SqlSession sqlSession = sqlSessionFactory.openSession(true);
////
////        final UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
////        final List<User> userList = userMapper.findAll();
////
////        sqlSession.close();
////        return userList;
//    }
//
//    public List<User> getConditionsUser() throws IOException {
////        User user = new User();
////        user.setId(6);
////        user.setUsername("test-delete");
////
////        final InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
////        final SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
////        final SqlSession sqlSession = sqlSessionFactory.openSession(true);
////        final UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
////        final List<User> userList = userMapper.findByCondition(user);
////
////        sqlSession.close();
////
////        return userList;
//    }
//
//    public List<User> findUserByIds(int[] ids) throws IOException {
////        final InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
////        final SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
////        final SqlSession sqlSession = sqlSessionFactory.openSession(true);
////        final UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
////        final List<User> user = userMapper.findByIds(ids);
////        sqlSession.close();
////        return user;
//    }
}
